Skip to content

Conversation

BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Oct 2, 2025

This PR attempts to constant fold the index value in translate_index_expr

I'm not sure any test changes are warranted for a small PR of this nature.

if (
isinstance(base.type, RTuple)
and isinstance(folded_index := constant_fold_expr(builder, index), int)
and folded_index >= 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for index overflow (it may generate an error elsewhere, but we don't want to crash the compiler if it's ignored).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I also just thought about reverse indexing, I'll implement that too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to adapt TupleGet before we can pass negative values here.

I've implemented that in a separate PR #19990 , lets pause this one until we get that merged in so we don't have to come back to this section later

BobTheBuidler and others added 10 commits October 2, 2025 13:31
maybe I should just compute the correct positive index value when we encounter a negative index, rather than doing this? 

Where's the best place to do that? Should that be with this or in a separate PR?
BobTheBuidler added a commit to BobTheBuidler/mypy that referenced this pull request Oct 2, 2025
This PR modifies `TupleGet.__init__` to automatically convert negative indexes to positive indexes instead of crashing at the assert

This won't change functionality on its own, but will allow us to pass negative values in python#19972 so I think we should consider this PR a prerequisite to that one
BobTheBuidler added a commit to BobTheBuidler/mypy that referenced this pull request Oct 2, 2025
This PR modifies `TupleGet.__init__` to automatically convert negative indexes to positive indexes instead of crashing at the assert

This won't change functionality on its own, since none of the existing calling locations can pass a negative value, but will allow us to pass negative values in python#19972 so I think we should consider this PR a prerequisite to that one
@BobTheBuidler

This comment was marked as off-topic.

p-sawicki added a commit that referenced this pull request Oct 7, 2025
This PR modifies `TupleGet.__init__` to automatically convert negative
indexes to positive indexes instead of crashing at the assert

This won't change functionality on its own, since none of the existing
calling locations can pass a negative value, but will allow us to pass
negative values in #19972 so I think
we should consider this PR a prerequisite to that one

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Piotr Sawicki <[email protected]>
@BobTheBuidler
Copy link
Contributor Author

Now that #19990 is merged, this PR is ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants